package com.imooc.io;

import java.io.UnsupportedEncodingException;

public class EncodeDemo {

	public static void main(String[] args) throws UnsupportedEncodingException {
		String s = "慕课MOOC";
		byte[] bytes = s.getBytes();
		
		for(byte b : bytes) {
			//转换成字节以16进制方式显示编码，当前编码是UTF-8
			System.out.print(Integer.toHexString(b & 0xff) + " ");
		}
		System.out.println();
		
		byte[] bytes2 = s.getBytes("gbk");
		for(byte b : bytes2) {
			//转换成gbk编码，中文占2个字节，字母占1个字节
			System.out.print(Integer.toHexString(b & 0xff) + " ");
		}
		System.out.println();
		byte[] bytes3 = s.getBytes("utf-8");
		for(byte b : bytes3) {
			//UTF-8编码，中文占3个字节，字母占1个字节
			System.out.print(Integer.toHexString(b & 0xff) + " ");
		}
		System.out.println();
		//java是双字节编码，utf-16be
		byte[] bytes4 = s.getBytes("utf-16be");
		for(byte b : bytes4) {
			//中文占2字节，字母占2字节
			System.out.print(Integer.toHexString(b & 0xff) + " ");
		}
	}

}
